package com.itheima.reggie.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.Pojo.ShoppingCart;
import com.itheima.reggie.Service.ShoppingCartService;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 展示购物车数据
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> select() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        Long userid = BaseContext.getCurrentId();
//      获取当前登录用户的购物车数据
        queryWrapper.eq(userid!=null,ShoppingCart::getUserId,userid);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 添加菜品或套餐
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
//        设置当前用户的id,指定当前是哪个用户的购物车
        Long userid = BaseContext.getCurrentId();
        shoppingCart.setUserId(userid);

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        if (shoppingCart.getDishId() != null) {
//        当前添加的是菜品，以菜品id为查询条件
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
//        当前添加的是套餐，以套餐id为查询条件
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
//        条件查询，以当前登录用户的id为查询条件
            queryWrapper.eq(ShoppingCart::getUserId,userid);
            ShoppingCart one = shoppingCartService.getOne(queryWrapper);
//        判断是否已有菜品(套餐)在数据库中
            if(one!=null){
//              是的话将该条记录的number属性加一
                one.setNumber(one.getNumber()+1);
                shoppingCartService.updateById(one);
            }else {
//              否的话新增一个购物车数据
                shoppingCartService.save(shoppingCart);
                one=shoppingCart;
            }

        return R.success(one);
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        Long userid = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userid!=null,ShoppingCart::getUserId,userid);
//      判断是菜品还是套餐
        if(shoppingCart.getDishId()!=null){
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
//      判断该菜品(套餐)的数量是否为1
        if(one.getNumber()==1){
            shoppingCartService.removeById(one);
        }else {
            one.setNumber(one.getNumber()-1);
            shoppingCartService.updateById(one);
        }

        return R.success("减少成功");
    }


    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> delete(){
        Long userid = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userid!=null,ShoppingCart::getUserId,userid);
        shoppingCartService.remove(queryWrapper);

        return R.success("购物车清空成功");
    }
}